package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dto.CommodityDTO;
import com.example.domain.Commodity;
import com.example.mapper.CommodityMapper;
import com.example.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 惠普
 * @description 针对表【commodity】的数据库操作Service实现
 * @createDate 2022-09-21 17:41:40
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity>
		implements CommodityService {

	/**
	 * 条件分页查询
	 */
	@Override
	public Page<Commodity> getCommodityByMIdAndTextForM(CommodityDTO dto) {
		Page<Commodity> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
		baseMapper.selectPage(page,
				new QueryWrapper<Commodity>().eq(ObjectUtils.isNotNull(dto.getUserId()), "user_id", dto.getUserId())
						.like(StringUtils.isNotBlank(dto.getText()), "text", dto.getText())
						.eq(ObjectUtils.isNotNull(dto.getAuditState()), "audit_state", dto.getAuditState())
						.eq(ObjectUtils.isNotNull(dto.getProductState()), "product_state", dto.getProductState()));
		return page;
	}

	/**
	 * 条件查询
	 */
	@Override
	public List<Commodity> getCommodityByConForU(CommodityDTO dto) {
		QueryWrapper<Commodity> query = new QueryWrapper<>();
		query.eq(ObjectUtils.isNotNull(dto.getIsCarousel()), "is_carousel", dto.getIsCarousel())
				.like(StringUtils.isNotBlank(dto.getText()), "text", dto.getText())
				.eq(ObjectUtils.isNotNull(dto.getAuditState()), "audit_state", dto.getAuditState())
				.eq(ObjectUtils.isNotNull(dto.getProductState()), "product_state", dto.getProductState());
		return baseMapper.selectList(query);
	}

	/**
	 * 获取商家的展示信息
	 */
	@Override
	public Map<String, Object> getMerchantInfoForU(Integer merchantId) {
		Map<String, Object> info = baseMapper.getMerchantInfoById(merchantId);
		if (info == null) {
			return null;
		}
		Commodity commodity = baseMapper.getMaxAppraiseByMerchantId(merchantId);
		info.put("picture", Base64.getEncoder().encodeToString(commodity.getPicture()));
		return info;
	}
}